<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Azure Queue Storage Message Count</title>
    <style>
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            margin: 0;
            padding: 20px;
            color: #333;
            max-width: 800px;
            margin: auto;
        }
        .question-container {
            background-color: #f8f9fa;
            border-radius: 8px;
            padding: 20px;
            margin-bottom: 20px;
        }
        .options {
            margin: 15px 0;
        }
        .option {
            padding: 10px;
            margin: 8px 0;
            background-color: white;
            border: 1px solid #ddd;
            border-radius: 4px;
            cursor: pointer;
        }
        .option:hover {
            background-color: #f0f7ff;
        }
        .option.selected {
            background-color: #e3f2fd;
            border-color: #0078D4;
        }
        button {
            background-color: #0078D4;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        button:hover {
            background-color: #106EBE;
        }
        #answer {
            display: none;
            margin-top: 20px;
            padding: 20px;
            background-color: #f9f9f9;
            border-radius: 8px;
        }
        .correct-answer {
            color: #107C10;
            font-weight: bold;
        }
        .explanation {
            margin-top: 15px;
        }
        .note {
            background-color: #fffde7;
            padding: 10px;
            border-left: 3px solid #ffd600;
            margin: 10px 0;
        }
        .code {
            background-color: #f5f5f5;
            padding: 10px;
            border-radius: 4px;
            font-family: Consolas, monospace;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <div class="question-container">
        <h2>QUESTION NO: 421</h2>
        
        <p>You have an Azure Queue Storage account that contains a queue named queue1.</p>
        <p>You plan to use Azure SDK for .NET to develop a solution that uses queue1.</p>
        <p>You need to author C# code that will return an approximate number of messages in queue1. Your solution must minimize the development effort.</p>
        <p>Which method should you use in your code?</p>
        
        <div class="options">
            <div class="option" onclick="selectOption(this)">A. GetProperties method of the QueueClient class</div>
            <div class="option" onclick="selectOption(this)">B. GetProperties method of the QueueServiceClient class</div>
            <div class="option" onclick="selectOption(this)">C. PeekMessages method of the QueueClient class</div>
            <div class="option" onclick="selectOption(this)">D. GetStatistics method of the QueueServiceClient class</div>
        </div>
    </div>
    
    <button onclick="showAnswer()">查看答案</button>
    
    <div id="answer">
        <h3 class="correct-answer">正确答案: A. GetProperties method of the QueueClient class</h3>
        
        <div class="explanation">
            <h4>说明:</h4>
            <p>在Azure Queue Storage中获取队列的近似消息数量时：</p>
            
            <table style="width:100%; border-collapse:collapse; margin:15px 0;">
                <tr>
                    <th style="text-align:left; padding:8px; border-bottom:1px solid #ddd;">方法</th>
                    <th style="text-align:left; padding:8px; border-bottom:1px solid #ddd;">用途</th>
                    <th style="text-align:left; padding:8px; border-bottom:1px solid #ddd;">效率</th>
                </tr>
                <tr>
                    <td style="padding:8px; border-bottom:1px solid #ddd;"><strong>QueueClient.GetProperties</strong></td>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">获取队列属性，包括ApproximateMessagesCount</td>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">最高效，只需1次API调用</td>
                </tr>
                <tr>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">QueueServiceClient.GetProperties</td>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">获取存储账户属性</td>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">不适用，无法获取队列消息数</td>
                </tr>
                <tr>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">QueueClient.PeekMessages</td>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">查看消息但不删除</td>
                    <td style="padding:8px; border-bottom:1px solid #ddd;">低效，需要获取所有消息</td>
                </tr>
                <tr>
                    <td style="padding:8px;">QueueServiceClient.GetStatistics</td>
                    <td style="padding:8px;">获取存储账户统计信息</td>
                    <td style="padding:8px;">不适用，无法获取队列消息数</td>
                </tr>
            </table>
            
            <div class="note">
                <p><strong>关键区别：</strong></p>
                <ul>
                    <li><code>QueueClient.GetProperties</code>直接返回包含<code>ApproximateMessagesCount</code>的队列元数据</li>
                    <li>其他方法要么无法获取消息数，要么需要处理实际消息内容</li>
                    <li>"近似"计数是因为队列的高可用性设计，消息可能在获取后几秒内才完全更新</li>
                </ul>
            </div>
            
            <p><strong>代码示例：</strong></p>
            <div class="code">
                <pre>
// 使用Azure.Storage.Queues包（v12.x+）
using Azure.Storage.Queues;

// 创建QueueClient实例
QueueClient queueClient = new QueueClient(
    "DefaultEndpointsProtocol=https;AccountName=yourAccount;AccountKey=yourKey;EndpointSuffix=core.windows.net",
    "queue1");

// 获取队列属性
QueueProperties properties = queueClient.GetProperties();

// 获取近似消息数
int approximateMessageCount = properties.ApproximateMessagesCount;
Console.WriteLine($"Approximate message count: {approximateMessageCount}");</pre>
            </div>
            
            <h4>最佳实践：</h4>
            <ol>
                <li>使用<code>QueueClient</code>而非<code>QueueServiceClient</code>操作特定队列</li>
                <li>缓存结果避免频繁调用（消息数更新可能有延迟）</li>
                <li>对于精确计数需求，考虑使用Service Bus而非Queue Storage</li>
            </ol>
        </div>
    </div>
    
    <script>
        function selectOption(element) {
            document.querySelectorAll('.option').forEach(opt => {
                opt.classList.remove('selected');
            });
            element.classList.add('selected');
        }
        
        function showAnswer() {
            document.getElementById('answer').style.display = 'block';
            window.scrollTo({
                top: document.getElementById('answer').offsetTop,
                behavior: 'smooth'
            });
        }
    </script>
</body>
</html>
